Python 标准库 -sys 模块

Python 标准库 -sys 模块

sys 模块提供了对解释器使用或维护的一些对象以及与解释器交互屏藩的函数的访问。

os、sys、platform 三个模块的区别:

不知道为什么,Python 给我一种感觉,就是 Python 比 Java 更接近系统底层,不知道是不是因为 Python 比 Java 离 C 更近

模块内容

动态对象(动态变量):




静态对象(常量):

方法:

实践

简单实践如下:

import builtins
import sys

print("---------------------------------"+"sys.argv"+"---------------------------------")
for arg in sys.argv:
    print(arg)
print("---------------------------------"+"sys.path"+"---------------------------------")
for path in sys.path:
    print(path)
print("---------------------------------"+"sys.modules"+"---------------------------------")
for name,module in sys.modules.items():
    print(name,"-",module)

# 重定向 stdout
# sys.stdout = open("./sys_out.txt","w",encoding="utf-8")
# 从这里开始,往后的所有输出都会输出到 sys_out.txt 文件中,而不是控制台中
# print("输出")

print("---------------------------------"+"sys.builtin_module_names"+"---------------------------------")
for module_name in sys.builtin_module_names:
    print(module_name)
print("---------------------------------"+"sys.copyright"+"---------------------------------")
print(sys.copyright)
print("---------------------------------"+"sys.exec_prefix"+"---------------------------------")
print(sys.exec_prefix)
print("---------------------------------"+"sys.prefix"+"---------------------------------")
print(sys.prefix)
print("---------------------------------"+"sys.executable"+"---------------------------------")
print(sys.executable)
print("---------------------------------"+"sys.float_info"+"---------------------------------")
print(sys.float_info)
print("---------------------------------"+"sys.float_repr_style"+"---------------------------------")
print(sys.float_repr_style)
print("---------------------------------"+"sys.hexversion"+"---------------------------------")
print(sys.hexversion)
print("---------------------------------"+"sys.implementation"+"---------------------------------")
print(sys.implementation)
print("---------------------------------"+"sys.maxsize"+"---------------------------------")
print(sys.maxsize)
print("---------------------------------"+"sys.maxunicode"+"---------------------------------")
print(sys.maxunicode)
print("---------------------------------"+"sys.platform"+"---------------------------------")
print(sys.platform)
print("---------------------------------"+"sys.thread_info"+"---------------------------------")
print(sys.thread_info)
print("---------------------------------"+"sys.version"+"---------------------------------")
print(sys.version)
print("---------------------------------"+"sys.version_info"+"---------------------------------")
print(sys.version_info)
print("---------------------------------"+"sys.dllhandle"+"---------------------------------")
print(sys.dllhandle)
print("---------------------------------"+"sys.winver"+"---------------------------------")
print(sys.winver)
print("---------------------------------"+"sys.displayhook()"+"---------------------------------")
aa = "xiashuo.xyz"
sys.displayhook(12)
sys.displayhook(aa)
print(builtins._)
print("---------------------------------"+"sys.getprofile()"+"---------------------------------")
sys.getprofile()
print("---------------------------------"+"sys.getrefcount()"+"---------------------------------")
# 使用 sys.getrefcount() 方法来查看整数和字符串的引用次数结果会比较难以判定,因为Python对其有内存优化
print(sys.getrefcount(aa))
# 返回 3
print(sys.getrefcount(121545))
class Ref_test():
    pass
# 正常,只会返回 1,也就是除了sys.getrefcount引用之外没有人引用
print(sys.getrefcount(Ref_test()))
print("---------------------------------"+"sys.getsizeof()"+"---------------------------------")
# 对象不包含任何内容,但是依然占据了内存
print(sys.getsizeof(Ref_test()))
print("---------------------------------"+"sys.getrecursionlimit()"+"---------------------------------")
print(sys.getrecursionlimit())

输出:

---------------------------------sys.argv---------------------------------
E:\PythonProject\PythonLearn\package_learn\built_in\sys_test.py
---------------------------------sys.path---------------------------------
E:\PythonProject\PythonLearn\package_learn\built_in
E:\PythonProject\PythonLearn
D:\Jetbrain_APP\apps\PyCharm-P\ch-0\231.9225.15\plugins\python\helpers\pycharm_display
D:\Python\Python3.10.4\python310.zip
D:\Python\Python3.10.4\DLLs
D:\Python\Python3.10.4\lib
D:\Python\Python3.10.4
D:\Python\Python3.10.4\lib\site-packages
D:\Jetbrain_APP\apps\PyCharm-P\ch-0\231.9225.15\plugins\python\helpers\pycharm_matplotlib_backend
---------------------------------sys.modules---------------------------------
sys - <module 'sys' (built-in)>
builtins - <module 'builtins' (built-in)>
_frozen_importlib - <module '_frozen_importlib' (frozen)>
_imp - <module '_imp' (built-in)>
_thread - <module '_thread' (built-in)>
_warnings - <module '_warnings' (built-in)>
_weakref - <module '_weakref' (built-in)>
_io - <module '_io' (built-in)>
marshal - <module 'marshal' (built-in)>
nt - <module 'nt' (built-in)>
winreg - <module 'winreg' (built-in)>
_frozen_importlib_external - <module '_frozen_importlib_external' (frozen)>
time - <module 'time' (built-in)>
zipimport - <module 'zipimport' (frozen)>
_codecs - <module '_codecs' (built-in)>
codecs - <module 'codecs' from 'D:\\Python\\Python3.10.4\\lib\\codecs.py'>
encodings.aliases - <module 'encodings.aliases' from 'D:\\Python\\Python3.10.4\\lib\\encodings\\aliases.py'>
encodings - <module 'encodings' from 'D:\\Python\\Python3.10.4\\lib\\encodings\\__init__.py'>
encodings.utf_8 - <module 'encodings.utf_8' from 'D:\\Python\\Python3.10.4\\lib\\encodings\\utf_8.py'>
_signal - <module '_signal' (built-in)>
_abc - <module '_abc' (built-in)>
abc - <module 'abc' from 'D:\\Python\\Python3.10.4\\lib\\abc.py'>
io - <module 'io' from 'D:\\Python\\Python3.10.4\\lib\\io.py'>
__main__ - <module '__main__' from 'E:\\PythonProject\\PythonLearn\\package_learn\\built_in\\sys_test.py'>
_stat - <module '_stat' (built-in)>
stat - <module 'stat' from 'D:\\Python\\Python3.10.4\\lib\\stat.py'>
_collections_abc - <module '_collections_abc' from 'D:\\Python\\Python3.10.4\\lib\\_collections_abc.py'>
genericpath - <module 'genericpath' from 'D:\\Python\\Python3.10.4\\lib\\genericpath.py'>
ntpath - <module 'ntpath' from 'D:\\Python\\Python3.10.4\\lib\\ntpath.py'>
os.path - <module 'ntpath' from 'D:\\Python\\Python3.10.4\\lib\\ntpath.py'>
os - <module 'os' from 'D:\\Python\\Python3.10.4\\lib\\os.py'>
_sitebuiltins - <module '_sitebuiltins' from 'D:\\Python\\Python3.10.4\\lib\\_sitebuiltins.py'>
_codecs_cn - <module '_codecs_cn' (built-in)>
_multibytecodec - <module '_multibytecodec' (built-in)>
encodings.gbk - <module 'encodings.gbk' from 'D:\\Python\\Python3.10.4\\lib\\encodings\\gbk.py'>
itertools - <module 'itertools' (built-in)>
keyword - <module 'keyword' from 'D:\\Python\\Python3.10.4\\lib\\keyword.py'>
_operator - <module '_operator' (built-in)>
operator - <module 'operator' from 'D:\\Python\\Python3.10.4\\lib\\operator.py'>
reprlib - <module 'reprlib' from 'D:\\Python\\Python3.10.4\\lib\\reprlib.py'>
_collections - <module '_collections' (built-in)>
collections - <module 'collections' from 'D:\\Python\\Python3.10.4\\lib\\collections\\__init__.py'>
types - <module 'types' from 'D:\\Python\\Python3.10.4\\lib\\types.py'>
_functools - <module '_functools' (built-in)>
functools - <module 'functools' from 'D:\\Python\\Python3.10.4\\lib\\functools.py'>
enum - <module 'enum' from 'D:\\Python\\Python3.10.4\\lib\\enum.py'>
_sre - <module '_sre' (built-in)>
sre_constants - <module 'sre_constants' from 'D:\\Python\\Python3.10.4\\lib\\sre_constants.py'>
sre_parse - <module 'sre_parse' from 'D:\\Python\\Python3.10.4\\lib\\sre_parse.py'>
sre_compile - <module 'sre_compile' from 'D:\\Python\\Python3.10.4\\lib\\sre_compile.py'>
_locale - <module '_locale' (built-in)>
copyreg - <module 'copyreg' from 'D:\\Python\\Python3.10.4\\lib\\copyreg.py'>
re - <module 're' from 'D:\\Python\\Python3.10.4\\lib\\re.py'>
token - <module 'token' from 'D:\\Python\\Python3.10.4\\lib\\token.py'>
tokenize - <module 'tokenize' from 'D:\\Python\\Python3.10.4\\lib\\tokenize.py'>
linecache - <module 'linecache' from 'D:\\Python\\Python3.10.4\\lib\\linecache.py'>
traceback - <module 'traceback' from 'D:\\Python\\Python3.10.4\\lib\\traceback.py'>
sitecustomize - <module 'sitecustomize' from 'D:\\Jetbrain_APP\\apps\\PyCharm-P\\ch-0\\231.9225.15\\plugins\\python\\helpers\\pycharm_matplotlib_backend\\sitecustomize.py'>
site - <module 'site' from 'D:\\Python\\Python3.10.4\\lib\\site.py'>
---------------------------------sys.builtin_module_names---------------------------------
_abc
_ast
_bisect
_blake2
_codecs
_codecs_cn
_codecs_hk
_codecs_iso2022
_codecs_jp
_codecs_kr
_codecs_tw
_collections
_contextvars
_csv
_datetime
_functools
_heapq
_imp
_io
_json
_locale
_lsprof
_md5
_multibytecodec
_opcode
_operator
_pickle
_random
_sha1
_sha256
_sha3
_sha512
_signal
_sre
_stat
_statistics
_string
_struct
_symtable
_thread
_tracemalloc
_warnings
_weakref
_winapi
_xxsubinterpreters
array
atexit
audioop
binascii
builtins
cmath
errno
faulthandler
gc
itertools
marshal
math
mmap
msvcrt
nt
sys
time
winreg
xxsubtype
zlib
---------------------------------sys.copyright---------------------------------
Copyright (c) 2001-2022 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
---------------------------------sys.exec_prefix---------------------------------
D:\Python\Python3.10.4
---------------------------------sys.prefix---------------------------------
D:\Python\Python3.10.4
---------------------------------sys.executable---------------------------------
D:\Python\Python3.10.4\python.exe
---------------------------------sys.float_info---------------------------------
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
---------------------------------sys.float_repr_style---------------------------------
short
---------------------------------sys.hexversion---------------------------------
50988272
---------------------------------sys.implementation---------------------------------
namespace(name='cpython', cache_tag='cpython-310', version=sys.version_info(major=3, minor=10, micro=4, releaselevel='final', serial=0), hexversion=50988272)
---------------------------------sys.maxsize---------------------------------
9223372036854775807
---------------------------------sys.maxunicode---------------------------------
1114111
---------------------------------sys.platform---------------------------------
win32
---------------------------------sys.thread_info---------------------------------
sys.thread_info(name='nt', lock=None, version=None)
---------------------------------sys.version---------------------------------
3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]
---------------------------------sys.version_info---------------------------------
sys.version_info(major=3, minor=10, micro=4, releaselevel='final', serial=0)
---------------------------------sys.dllhandle---------------------------------
140720921116672
---------------------------------sys.winver---------------------------------
3.10
---------------------------------sys.displayhook()---------------------------------
12
'xiashuo.xyz'
xiashuo.xyz
---------------------------------sys.getprofile()---------------------------------
---------------------------------sys.getrefcount()---------------------------------
5
3
1
---------------------------------sys.getsizeof()---------------------------------
48
---------------------------------sys.getrecursionlimit()---------------------------------
1000

动态修改 path 变量

动态添加 C:/Users/wwwli/Desktop/script 到 path 中,其中此路径下有文件 aaa.py,内容如下:

abcd = "121312313"

class ABCD():
    pass

执行测试脚本

print("---------------------------------dynamic sys.path---------------------------------")
# 动态添加  C:/Users/wwwli/Desktop/script 到 path
sys.path.append("C:/Users/wwwli/Desktop/script")
from aaa import abcd
from aaa import ABCD
print(abcd)
print(ABCD())

输出:

---------------------------------dynamic sys.path---------------------------------
121312313
<aaa.ABCD object at 0x0000026A31CA7E80>

注意,虽然解释器报错,但是执行是没有问题的

当然,如果路径真的不存在,那在执行的时候就会报错了。